{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70a2b95e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import mlflow\n",
    "from sklearn import datasets\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from pathlib import Path\n",
    "\n",
    "X, y = datasets.load_iris(as_frame=True, return_X_y=True)\n",
    "model = KNeighborsClassifier()\n",
    "model.fit(X, y)\n",
    "\n",
    "model_path = \"/tmp/sk_model_01\"\n",
    "\n",
    "mlflow.sklearn.save_model(model, model_path)\n",
    "\n",
    "model_requirements_file_path = mlflow.pyfunc.get_model_dependencies(model_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "238d6445",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(Path(model_requirements_file_path).read_text())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f9b51b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install -r $model_requirements_file_path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b0a183b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# In order to enable the environment restored by %pip command above,\n",
    "# you need to manually click the kernel restart button."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1262da7f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import mlflow\n",
    "model = mlflow.pyfunc.load_model(\"/tmp/sk_model_01\")\n",
    "\n",
    "from sklearn import datasets\n",
    "X, y = datasets.load_iris(as_frame=True, return_X_y=True)\n",
    "result = model.predict(X)\n",
    "\n",
    "print(result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
